/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package questionbook.service;

import java.util.ArrayList;
import java.util.List;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import questionbook.model.Person;
import questionbook.service.common.GenericServiceImpl;
import questionbook.standard.Foaf;
import questionbook.standard.Qbook;

/**
 *
 * @author stefanstift
 */
public class FriendshipServiceBean extends GenericServiceImpl implements FriendshipService {

    PersonService personService = new PersonServiceBean();

    public void addFriendship(Person p1, Person p2) {
        this.storeManager.getStore().add(p1.getUri(), Foaf.getInstance().getKnowsURI(), p2.getUri());
        this.storeManager.getStore().add(p2.getUri(), Foaf.getInstance().getKnowsURI(), p1.getUri());
    }

    public void deleteFriendship(Person p1, Person p2) {
        this.storeManager.getStore().removeTuplePattern(p1.getUri(), Foaf.getInstance().getKnowsURI(), p2.getUri());
        this.storeManager.getStore().removeTuplePattern(p2.getUri(), Foaf.getInstance().getKnowsURI(), p1.getUri());
    }

    public void addInvitation(Person source, Person target) {
        this.storeManager.getStore().add(source.getUri(), Qbook.getInstance().getAsksFriendshipURI(), target.getUri());
    }

    public void confirmInvitation(Person target, Person source) {

        this.storeManager.getStore().removeTuplePattern(source.getUri(), Qbook.getInstance().getAsksFriendshipURI(), target.getUri());
        this.addFriendship(source, target);
    }

    public void declineInvitation(Person target, Person source) {
          this.storeManager.getStore().removeTuplePattern(source.getUri(), Qbook.getInstance().getAsksFriendshipURI(), target.getUri());
    }

    public List<Person> getInvitationsPersons(Person p) {

        List<Person> personList = new ArrayList<Person>();
        List<Statement> stmts = this.storeManager.getStore().tuplePattern(null, Qbook.getInstance().getAsksFriendshipURI(), p.getUri());

        for(Statement s : stmts)
        {
            URI u = (URI) s.getSubject();
            personList.add(personService.get(u));
        }

        return personList;
    }

    public List<Person> getInvitedPersons(Person p) {

        List<Person> personList = new ArrayList<Person>();
        List<Statement> stmts = this.storeManager.getStore().tuplePattern(p.getUri(), Qbook.getInstance().getAsksFriendshipURI(), null);

        for(Statement s : stmts)
        {
            URI u = (URI) s.getObject();
            personList.add(personService.get(u));
        }

        return personList;
    }

     public List<Person> getFriends(Person p) {

        List<Person> personList = new ArrayList<Person>();
        List<Statement> stmts = this.storeManager.getStore().tuplePattern(p.getUri(), Foaf.getInstance().getKnowsURI(), null);

        for(Statement s : stmts)
        {
            URI u = (URI) s.getObject();
            personList.add(personService.get(u));
        }
        
        return personList;
    }
}
